<%@ page contentType="text/html;charset=utf-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="com.appspot.niehonglei.jdo.Index" %>
<%@ page import="com.appspot.niehonglei.common.Tools" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="com.appspot.niehonglei.dao.PMF" %>
<%@ page import="com.appspot.niehonglei.jdo.Comment" %>
<%@ page import="com.appspot.niehonglei.jdo.Common" %>

<%

		//处理分类
		String action = request.getParameter("_action");
		String countIndexAction = "";
		Map category = new HashMap();
		category.put("internet","互联网");
		category.put("java","Java");
		category.put("search","搜索");
		category.put("js","Js");
		category.put("life","生活");
		category.put("互联网","internet");
		category.put("Java","java");
		category.put("搜索","search");
		category.put("Js","js");
		category.put("生活","life");		
		String cate = (String)category.get(action);
		if(cate == null){
			cate = "";
			action = "";
		} else {
			countIndexAction = cate;
			cate = " where category == '" + cate + "' ";
		}
		
		
		//处理分页
		String footAction = action;
		if(!"".equals(footAction)){
			footAction += "/";
		}
		String pageNo = request.getParameter("_page");
		int intPageNo = 1;
		int recordsPerPage = 10;
		try{
			intPageNo = Integer.parseInt(pageNo);
		} catch (Exception e){
			//e.printStackTrace();
		}
		int totalRecords = Tools.countIndex(countIndexAction);
		//防止传入的参数为负数
		if(intPageNo <= 0){
			intPageNo = 1;
		}

		//计算总页数
		int totalPageNo = 1;		
		if(totalRecords % recordsPerPage == 0){
			totalPageNo = totalRecords / recordsPerPage;
		} else {
			totalPageNo = (totalRecords / recordsPerPage) + 1;
		}
		//防止总记录数为0
		if(totalPageNo == 0){
			totalPageNo = 1;
		}
		//控制是否在正常的上限范围内
		if((intPageNo - 1) * recordsPerPage > totalRecords){
			intPageNo = totalPageNo;
		}
		

		//生成页码
		String footCode = "";
		//小于5 直接生成1 2 3 4 5
		if(totalPageNo <= 8){
			for(int i=0;i<totalPageNo;i++){
				footCode += "<a href='/" + footAction + "page/"  + (i + 1) + "' class='blackText'>" + (i + 1) + "</a> ";
			}			
		}
	
		if(totalPageNo > 8 && intPageNo < 5){
			for(int i=0;i<5;i++){
				footCode += "<a href='/" + footAction + "page/"  + (i + 1) + "' class='blackText'>" + (i + 1) + "</a> ";
			}
			footCode += "<a href='/" + footAction + "page/"  + ((totalPageNo - 5)/2 + 5) + "' class='blackText'>...</a> ";
			footCode += " <a href='/" + footAction + "page/"  + totalPageNo + "' class='blackText'>末页</a>";			
		}		
		
		else if(totalPageNo > 8 && intPageNo >= 5 && (intPageNo <= totalPageNo - 4)){
			footCode +=  "<a href='/" + footAction + "page/"  + 1 + "' class='blackText'>首页</a> ";
			footCode += "<a href='/" + footAction + "page/"  + ((intPageNo-2)/2 + 1) + "' class='blackText'>...</a> ";
			for(int i=0;i<5;i++){
				footCode += "<a href='/" + footAction + "page/"  + (intPageNo + i - 2) + "' class='blackText'>" + (intPageNo + i - 2) + "</a> ";
			}
			footCode += "<a href='/" + footAction + "page/"  + ((totalPageNo - intPageNo)/2 + 1 + intPageNo) + "' class='blackText'>...</a> ";
			footCode += " <a href='/" + footAction + "page/"  + totalPageNo + "' class='blackText'>末页</a>";
		}
		
		
		else if(totalPageNo > 8 && (intPageNo > totalPageNo - 4)){
			footCode +=  "<a href='/" + footAction + "page/"  + 1 + "' class='blackText'>首页</a> ";
			footCode += "<a href='/" + footAction + "page/"  + ((totalPageNo - 4)/2) + "' class='blackText'>...</a> ";
			for(int i=0;i<5;i++){
				footCode += " <a href='/" + footAction + "page/"  + (totalPageNo + i - 4) + "' class='blackText'>" + (totalPageNo + i - 4) + "</a> ";
			}			
		}

		
		String range = " range " + (intPageNo - 1) * recordsPerPage + "," + intPageNo * recordsPerPage;

%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="Shortcut icon" href="/favicon.ico" />
<link rel="Bookmark" href="/favicon.ico"> 
<title>Nie's Home</title>
<script language="JavaScript" src="http://www.google.com/jsapi"></script>
<script language="JavaScript">
	google.load("jquery","1");
</script>
<script language="JavaScript" src="/js/index.js"></script>
<style type="text/css">
<!--
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
	font-size: 12px;
	line-height: 22px;
}
.sideTitle {
	font-size: 14px;
	font-weight: bold;
	color: #E25209;
}
.basicInfo {
	color: #999;
}
.whiteText {
	color: #FFF;
}
.blackText {
	color: #000;
}
.title {
	font-size: 18px;
	font-weight: bold;
	color: #E25209;
}

a.whiteText{

}
a{
	text-decoration:none;
}
a:hover{
	text-decoration:underline;
}
.content {
}
-->
</style></head>

<body onload="MM_preloadImages('images/index_06_2.gif')">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td height="92" align="center" background="images/index_01.gif"><table width="987" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="27">&nbsp;</td>
        <td width="134"><a href="/"><img src="images/index_04.gif" alt="Nie's Home" border="0" longdesc="http://niehonglei.appspot.com" /></a></td>
        <td width="58" valign="middle">&nbsp;</td>

        <!--分类模板开始-->
        <td width="79" align="center" background="images/index_06.gif" onmouseover="swampImage(this,'images/index_06_2.gif')" onmouseout="swampImage(this,'images/index_06.gif')"><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="13%" height="41" rowspan="3">&nbsp;</td>
            <td width="76%" height="7"></td>
            <td width="11%" rowspan="3">&nbsp;</td>
          </tr>
          <tr>
            <td height="12" align="center" valign="top" class="whiteText"><a href="/internet" class="whiteText">互联网</a></td>
          </tr>
          <tr>
            <td height="12"></td>
          </tr>

        </table></td>
        <!--分类模板结束-->
        <td width="30">&nbsp;</td>
       <!--分类模板开始-->
        <td width="79" align="center" background="images/index_06.gif" onmouseover="swampImage(this,'images/index_06_2.gif')" onmouseout="swampImage(this,'images/index_06.gif')"><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="13%" height="41" rowspan="3">&nbsp;</td>
            <td width="76%" height="7"></td>
            <td width="11%" rowspan="3">&nbsp;</td>
          </tr>
          <tr>
            <td height="12" align="center" valign="top"><a href="/java" class="whiteText">Java</a></td>
          </tr>
          <tr>
            <td height="12"></td>
          </tr>

        </table></td>
        <!--分类模板结束-->
        <td width="30">&nbsp;</td>
       <!--分类模板开始-->
        <td width="79" align="center" background="images/index_06.gif" onmouseover="swampImage(this,'images/index_06_2.gif')" onmouseout="swampImage(this,'images/index_06.gif')"><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="13%" height="41" rowspan="3">&nbsp;</td>
            <td width="76%" height="7"></td>
            <td width="11%" rowspan="3">&nbsp;</td>
          </tr>
          <tr>
            <td height="12" align="center" valign="top"><a href="/search" class="whiteText">搜索</a></td>
          </tr>
          <tr>
            <td height="12"></td>
          </tr>

        </table></td>
        <!--分类模板结束-->
        <td width="30">&nbsp;</td>
       <!--分类模板开始-->
        <td width="79" align="center" background="images/index_06.gif" onmouseover="swampImage(this,'images/index_06_2.gif')" onmouseout="swampImage(this,'images/index_06.gif')"><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="13%" height="41" rowspan="3">&nbsp;</td>
            <td width="76%" height="7"></td>
            <td width="11%" rowspan="3">&nbsp;</td>
          </tr>
          <tr>
            <td height="12" align="center" valign="top"><a href="/js" class="whiteText">Js</a></td>
          </tr>
          <tr>
            <td height="12"></td>
          </tr>

        </table></td>
        <!--分类模板结束-->
        <td width="30">&nbsp;</td>
       <!--分类模板开始-->
        <td width="79" align="center" background="images/index_06.gif" onmouseover="swampImage(this,'images/index_06_2.gif')" onmouseout="swampImage(this,'images/index_06.gif')"><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="13%" height="41" rowspan="3">&nbsp;</td>
            <td width="76%" height="7"></td>
            <td width="11%" rowspan="3">&nbsp;</td>
          </tr>
          <tr>
            <td height="12" align="center" valign="top"><a href="/life" class="whiteText">生活</a></td>
          </tr>
          <tr>
            <td height="12"></td>
          </tr>

        </table></td>
        <!--分类模板结束-->
        <td>&nbsp;</td>
      </tr>
    </table></td>
  </tr>

  <tr>
    <td height="800" align="center" valign="top" bgcolor="#E5E2DD"><table width="987" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="722" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td height="67" background="images/index_11.gif"><table width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td height="33" colspan="3">&nbsp;</td>
              </tr>
              <tr>
                <td width="2%" height="34">&nbsp;</td>
                <td width="95%" align="left"  class="whiteText">热门文章：
                <%{//使用局部变量
				    PersistenceManager pm = PMF.get().getPersistenceManager();
					String rankingQuery = "select from " + Common.class.getName() + 
							" where type == 0 order by createDate desc";
					List<Common> rankingList = (List<Common>) pm.newQuery(rankingQuery)
							.execute();
					if(rankingList==null) rankingList = new ArrayList();				
					for(int i=0;i<rankingList.size();i++){
						Common ranking = rankingList.get(i);
						String outprint = "";
						if(i!=0){
							outprint += " | ";
						}
						outprint += "<a href='" + ranking.getLink() + "' class='whiteText' >";
						outprint += ranking.getTitle();
						outprint += "</a>";
						out.print(outprint);
					}
					pm.close();
				}
				%>			
				</td>
                <td width="3%">&nbsp;</td>
              </tr>
            </table></td>
          </tr>
        <%{//局部变量开始
			PersistenceManager pm = PMF.get().getPersistenceManager();
			String query = "select from " + Index.class.getName() + cate + " order by createDate desc" + range;
			List<Index> list = (List<Index>) pm.newQuery(query).execute();
			if(list==null) list = new ArrayList();
				for(int i=0;i<list.size();i++){
					Index index = list.get(i);
		%>          
          <!--文章模板开始-->
          <tr>
            <td height="13" background="images/index_14.gif"></td>
          </tr>
          <tr>
            <td  background="images/index_16.gif"><table width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td colspan="2">&nbsp;</td>
                <td width="95%" align="left"><a href="/detail/<%= index.getId().intValue() %>" class="title"><%= index.getTitle() %></a></td>
                <td>&nbsp;</td>
              </tr>
              <tr>
                <td height="5" colspan="2"></td>
                <td></td>
                <td></td>
              </tr>              
              <tr>
                <td colspan="2"></td>
                <td background="images/line.gif" height="1"></td>
                <td></td>
                </tr>
              <tr>
                <td height="5" colspan="2"></td>
                <td></td>
                <td></td>
              </tr>
              <tr>
                <td colspan="2">&nbsp;</td>
                <td align="left" class="basicInfo">分类： <a href="/<%= category.get(index.getCategory()) %>" class="basicInfo"><%= index.getCategory() %></a> - <%= index.getAuthor() %> @ <%= Tools.date2str(index.getCreateDate()) %> 评论： <a href="/detail/<%= index.getId().intValue() %>#comment" class="basicInfo"><%= Tools.countComment(index.getId().intValue() + "")%></a>条 点击：<%= index.getClickRate()==null?0:index.getClickRate().intValue()%> <%= session.getAttribute("logName")==null?"":"<a href='/action/editIndex&id="+index.getId().intValue()+"' class='basicInfo'>编辑</a> <a href='/action/deleteIndex&id="+index.getId().intValue()+"' class='basicInfo'>删除</a>"%></td>
                <td>&nbsp;</td>
              </tr>
              <tr>
                <td colspan="2">&nbsp;</td>
                <td align="left">&nbsp;</td>
                <td>&nbsp;</td>
              </tr>
              <tr>
                <td width="2%"><p>&nbsp;</p></td>
                <td colspan="2" align="left" class="content"><%= index.getContent().getValue() %></td>
                <td width="1%">&nbsp;</td>
              </tr>
            </table></td>
          </tr>
          <tr>
            <td height="11"  background="images/index_20.gif"></td>
          </tr>
		  <!--文章模板结束-->
        <%
				}
				//关闭链接
				pm.close();
		}%>          
           <!--分页模板开始-->
          <tr>
            <td height="13" background="images/index_14.gif"></td>
          </tr>
          <tr>
            <td  background="images/index_16.gif"><table width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td width="2%">&nbsp;</td>
                <td width="97%" align="left">当前是第<%= intPageNo %>页 共<%= totalPageNo %>页 &nbsp;<%= footCode %></td>
                <td width="1%">&nbsp;</td>
              </tr>
              </table></td>
          </tr>
          <tr>
            <td height="11"  background="images/index_20.gif"></td>
          </tr>
		  <!--分页模板结束-->
                    
        </table></td>
        <td align="center" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td>&nbsp;</td>
          </tr>
          <!--左边模板开始-->  
          <tr>
            <td height="12" background="images/index_18.gif"></td>
          </tr>
          <tr>
            <td  background="images/index_22.gif"><table width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td>&nbsp;</td>
                <td align="left" class="sideTitle"><a href="/rss"><img src="images/sub_rss.gif" alt="订阅" width="16" height="16" border="0" align="absmiddle" /></a>订阅</td>
                <td>&nbsp;</td>
              </tr>
              <tr>
                <td height="5"></td>
                <td></td>
                <td></td>
              </tr> 
              <tr>
                <td></td>
                <td background="images/line.gif" height="1"></td>
                <td></td>
               </tr> 
                                           
              <tr>
                <td width="6%">&nbsp;</td>
                <td width="91%" align="left">跟随我的twitter<a href="http://twitter.com/nehnre"><img src="images/sub_twitter.gif" alt="twitter" width="16" height="16" border="0" align="absmiddle" /></a><br />跟随我的新浪微博
                  <a href="http://t.sina.com.cn/nehnre"><img src="images/sina_t.gif" alt="新浪微博" width="16" height="16" border="0" align="absmiddle" /></a><br />跟随我的嘀咕
                  <a href="http://digu.com/nehnre"><img src="images/digu_t.gif" alt="嘀咕" width="16" height="16" border="0" align="absmiddle" /></a></td>
                <td width="3%">&nbsp;</td>
              </tr>
            </table></td>
          </tr>
          <tr>
            <td height="12"  background="images/index_24.gif"></td>
          </tr>
          <!--左边模板结束-->  
          <!--左边模板开始-->  
          <tr>
            <td height="12" background="images/index_18.gif"></td>
          </tr>
          <tr>
            <td  background="images/index_22.gif"><table width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td width="6%">&nbsp;</td>
                <td align="left" width="91%">
					<form action="http://www.google.com/cse" id="cse-search-box">
					  <div>
						<input type="hidden" name="cx" value="007282536651609248827:t79dpyawyjs" />
						<input type="hidden" name="ie" value="UTF-8" />
						<input type="text" name="q" size="20"/>
						<input type="submit" name="sa" value="搜索" />
					  </div>
					</form>
					<script type="text/javascript" src="http://www.google.com/cse/brand?form=cse-search-box&lang=zh-Hans"></script>						
				</td>
                <td width="3%">&nbsp;</td>
              </tr>
            </table></td>
          </tr>
          <tr>
            <td height="12"  background="images/index_24.gif"></td>
          </tr>
          <!--左边模板结束-->  
          <!--左边模板开始-->  
          <tr>
            <td height="12" background="images/index_18.gif"></td>
          </tr>
          <tr>
            <td  background="images/index_22.gif"><table width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td>&nbsp;</td>
                <td align="left" class="sideTitle">★关于本站</td>
                <td>&nbsp;</td>
              </tr>
              <tr>
                <td height="5"></td>
                <td></td>
                <td></td>
              </tr> 
              <tr>
                <td></td>
                <td background="images/line.gif" height="1"></td>
                <td></td>
               </tr> 
                                           
              <tr>
                <td width="6%">&nbsp;</td>
                <td width="91%" align="left">本站依托于google app engine，程序爱好者“发奋”的地方。邮件联系：adminATniehongleiDOTinfo（请自动转换）</td>
                <td width="3%">&nbsp;</td>
              </tr>
            </table></td>
          </tr>
          <tr>
            <td height="12"  background="images/index_24.gif"></td>
          </tr>
          <!--左边模板结束-->  
          <!--左边模板开始-->  
          <tr>
            <td height="12" background="images/index_18.gif"></td>
          </tr>
          <tr>
            <td  background="images/index_22.gif"><table width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td>&nbsp;</td>
                <td align="left" class="sideTitle">★最新评论</td>
                <td>&nbsp;</td>
              </tr>
              <tr>
                <td height="5"></td>
                <td></td>
                <td></td>
              </tr> 
              <tr>
                <td></td>
                <td background="images/line.gif" height="1"></td>
                <td></td>
               </tr> 
                                            
              <tr>
                <td width="6%">&nbsp;</td>
                <td width="91%" align="left">
                <%{//使用局部变量
				PersistenceManager pm = PMF.get().getPersistenceManager();
					String rankingQuery = "select from " + Comment.class.getName() + 
							" where ifPublish == 'true' order by createDate desc range 0,20";
					List<Comment> rankingList = (List<Comment>) pm.newQuery(rankingQuery)
							.execute();
					if(rankingList==null) rankingList = new ArrayList();				
					for(int i=0;i<rankingList.size();i++){
						Comment ranking = rankingList.get(i);
						String outprint = "<a href='/detail/" + ranking.getIndexId().intValue() + "' class='blackText' title='" + ranking.getContent().getValue() + "'>";
						if(ranking.getContent().getValue().length()>20){
							outprint += ranking.getContent().getValue().substring(0,15) + "...";
						} else {
							outprint += ranking.getContent().getValue();
						}
						outprint += "</a><br />";
						out.print(outprint);
					}
					pm.close();
				}
				%>
                </td>
                <td width="3%">&nbsp;</td>
              </tr>
            </table></td>
          </tr>
          <tr>
            <td height="12"  background="images/index_24.gif"></td>
          </tr>
          <!--左边模板结束-->                       
          <!--左边模板开始-->  
          <tr>
            <td height="12" background="images/index_18.gif"></td>
          </tr>
          <tr>
            <td  background="images/index_22.gif"><table width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td>&nbsp;</td>
                <td align="left" class="sideTitle">★友情链接</td>
                <td>&nbsp;</td>
              </tr>
              <tr>
                <td height="5"></td>
                <td></td>
                <td></td>
              </tr> 
              <tr>
                <td></td>
                <td background="images/line.gif" height="1"></td>
                <td></td>
               </tr> 
                                           
              <tr>
                <td width="6%">&nbsp;</td>
                <td width="91%" align="left">
				
                <%{//使用局部变量
				    PersistenceManager pm = PMF.get().getPersistenceManager();
					String rankingQuery = "select from " + Common.class.getName() + " where type == 1 order by createDate desc";
					List<Common> rankingList = (List<Common>) pm.newQuery(rankingQuery).execute();
					if(rankingList==null) rankingList = new ArrayList();				
					for(int i=0;i<rankingList.size();i++){
						Common ranking = rankingList.get(i);
						String outprint = "<a href='" + ranking.getLink() + "' >";
						outprint += ranking.getTitle();
						outprint += "</a><br />";
						out.print(outprint);
					}
					pm.close();
				}
				%>	
				</td>
                <td width="3%">&nbsp;</td>
              </tr>
            </table></td>
          </tr>
          <tr>
            <td height="12"  background="images/index_24.gif"></td>
          </tr>
          <!--左边模板结束-->  
          <!--左边模板开始-->  
          <tr>
            <td height="12" background="images/index_18.gif"></td>
          </tr>
          <tr>
            <td  background="images/index_22.gif"><table width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td>&nbsp;</td>
                <td align="left" class="sideTitle">★阅读排行</td>
                <td>&nbsp;</td>
              </tr>
              <tr>
                <td height="5"></td>
                <td></td>
                <td></td>
              </tr> 
              <tr>
                <td></td>
                <td background="images/line.gif" height="1"></td>
                <td></td>
               </tr> 
                                            
              <tr>
                <td width="6%">&nbsp;</td>
                <td width="91%" align="left">
                <%{//使用局部变量
					PersistenceManager pm = PMF.get().getPersistenceManager();
					String rankingQuery = "select from " + Index.class.getName() + 
							" order by clickRate desc, createDate desc range 0,10";
					List<Index> rankingList = (List<Index>) pm.newQuery(rankingQuery)
							.execute();
					if(rankingList==null) rankingList = new ArrayList();				
					for(int i=0;i<rankingList.size();i++){
						Index ranking = rankingList.get(i);
						String outprint = "<a href='/detail/" + ranking.getId().intValue() + "' class='blackText' title='" + ranking.getTitle() + "'>";
						if(ranking.getTitle().length()>20){
							outprint += ranking.getTitle().substring(0,15) + "...";
						} else {
							outprint += ranking.getTitle();
						}
						outprint += "</a><br />";
						out.print(outprint);
					}
					pm.close();
				}
				%>
                </td>
                <td width="3%">&nbsp;</td>
              </tr>
            </table></td>
          </tr>
          <tr>
            <td height="12"  background="images/index_24.gif"></td>
          </tr>
          <!--左边模板结束-->                              
        </table></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td height="71" valign="top" background="images/index_30.gif"><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td align="center" class="whiteText">powered by nehnre | 转载请注明出处 | 感谢appspot.com的大力支持 | <a href="/login" class="whiteText">管理员登录</a></td>
      </tr>
    </table></td>
  </tr>  
</table>
</body>
</html>
